package com.cloud.cloudorder.order.consumer;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 54485
 */
@Component
@Slf4j
//@RabbitListener(queues = RabbitmqOrderConstants.ORDER_DIRECT_QUEUE)
public class DirectReceiver {

    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    @RabbitHandler(isDefault = true)
    public void process(JSONObject order, Channel channel, Message message) throws IOException {

        System.out.println("DirectReceiver消费者收到消息  : " +  atomicInteger.getAndIncrement());


        /**
         * Delivery Tag 用来标识信道中投递的消息。RabbitMQ 推送消息给 Consumer 时，会附带一个 Delivery Tag，
         * 以便 Consumer 可以在消息确认时告诉 RabbitMQ 到底是哪条消息被确认了。
         * RabbitMQ 保证在每个信道中，每条消息的 Delivery Tag 从 1 开始递增。
         */
        /**
         *  multiple 取值为 false 时，表示通知 RabbitMQ 当前消息被确认
         *  如果为 true，则额外将比第一个参数指定的 delivery tag 小的消息一并确认
         */

        System.out.println("消息正常消费" + message.getMessageProperties().getDeliveryTag());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        System.out.println("receiver success");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("消息ID：" + deliveryTag);
    }

}